<?php
class CvsController extends AppController {

	var $name = 'Cvs';
        var $components = array('Auth', 'FileUpload', 'RequestHandler');
        function  beforeFilter() {
            $this->FileUpload->fileModel = null;
            $this->FileUpload->fileVar = "picture";
            $this->FileUpload->allowedTypes = array('image/jpeg','image/gif','image/png','image/pjpeg','image/x-png');
            if(($this->Auth->user()) == null) :
                $this->redirect(array('controller'=>'users', 'action'=>'login'));
            endif;
        }

	function index($id = null) {
            $id_user = $this->Session->read("Auth.User.id");
            $privilege_id = $this->Session->read("Auth.User.privilege_id");
            if($privilege_id != 1) :
                $this->Cv->recursive = 0;
                $this->set('cvs', $this->paginate());
            else :
               $this->redirect(array('action'=>'user', $this->Session->read('Auth.User.id')));
            endif;
	}

	function user($id = null) {
            $id_user = $this->Session->read("Auth.User.id");
            $users = $this->Cv->User->findById($id);
            if($id != null && ($this->Session->read('Auth.User.id') == $id || $this->Session->read('Auth.User.id') == $users['User']['parent_id'])) :
                $cv_by_user = $this->paginate(array('Cv.user_id =' => $id));
            else :
               $this->redirect(array('controller'=>'users', 'action'=>'index'));
            endif;
            $this->set('cv_by_user', $this->paginate(array('Cv.user_id =' => $id)));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cv', true));
			$this->redirect(array('action' => 'index'));
		}
                $unCV = $this->Cv->findById($id);
                $users = $this->Cv->User->findById($unCV['Cv']['user_id']);
                $charge = $this->Cv->User->findById($users['User']['parent_id']);
		$this->set('cv', $this->Cv->read(null, $id));
		$this->set('users', $users);
		$this->set('charge', $charge);

	}
        
	function view_popup($id = null) {
                //window.open(this.href,'mentions','width=300,height=500,scrollbars=yes');return false
                $this->layout = 'none';
		if (!$id) {
			$this->Session->setFlash(__('Invalid cv', true));
			$this->redirect(array('action' => 'index'));
		}
                $unCV = $this->Cv->findById($id);
                $users = $this->Cv->User->findById($unCV['Cv']['user_id']);
                $charge = $this->Cv->User->findById($users['User']['parent_id']);
		$this->set('cv', $this->Cv->read(null, $id));
		$this->set('users', $users);
		$this->set('charge', $charge);

	}

	function add() {
		if (!empty($this->data)) {

                    $user = $this->Session->read('Auth.User');
                    $this->Cv->create();

                    //Les compétences

                    if(!empty($this->data['Skill']['label'])):
                        $exp = explode(';', $this->data['Skill']['label']);
                        foreach ($exp as $skill):
                            if(!empty ($skill)) :
                                $idSkill = $this->requestAction(array(
                                                        'controller' => 'Skills',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $skill
                                                                    )));
                                $skills[] = $idSkill;
                            endif;
                        endforeach;
                        $this->data['Skill'] = $skills;
                    endif;

                    //Les langues

                    if(!empty ($this->data['Language']['label'])) :
                            $idLangue[] = $this->requestAction(array(
                                                        'controller' => 'languages',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $this->data['Language']['label'],
                                                                        $this->data['Language']['level']
                                                                    )));

                     endif;
                    if(!empty ($this->data['Language2']['label'])) :
                            $idLangue[] = $this->requestAction(array(
                                                        'controller' => 'languages',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $this->data['Language2']['label'],
                                                                        $this->data['Language2']['level']
                                                                    )));

                     endif;
                    if(!empty ($this->data['Language3']['label'])) :
                            $idLangue[] = $this->requestAction(array(
                                                        'controller' => 'languages',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $this->data['Language3']['label'],
                                                                        $this->data['Language3']['level']
                                                                    )));

                     endif;
                     unset($this->data['Language']);
                     unset($this->data['Language2']);
                     unset($this->data['Language3']);
                     if(isset ($idLangue))
                        $this->data['Language'] = $idLangue;


                     if(!empty($this->data['Cv']['file']['type'])) :
                         print_r($this->data['Cv']['file']);
                        if((strpos($this->data['Cv']['file']['name'], '.pdf') >= 0 || strpos($this->data['Cv']['file']['name'], '.doc') >= 0) && $this->data['Cv']['file']['size'] <= 5000000) :
                                // Renomage de l'image et Upload de l'image
                                $exp = explode('.', $this->data['Cv']['file']['name']);
                                $ext = $exp[count($exp)-1];
                                $this->data['Cv']['import'] = $this->Cv->id.'.'.$ext;
                                if($this->Cv->save($this->data)) :
                                    $this->FileUpload->renameFile($this->data['Cv']['file']['name'], $this->data['Cv']['import']);
                                endif;
                             endif;
                      endif;
					  
					  

                    // Les catégories
                    if(isset($this->data['Category']['id']['0'])) :
                        $lesCategories = $this->data['Category']['id'];
                        unset ($this->data['Category']);
                        $this->data['Category'] = $lesCategories;
                    endif;


                    $this->data['Cv']['user_id'] = $user['id'];
                    if(!empty($this->data['Cv']['picture']['type']) && strpos($this->data['Cv']['picture']['type'], 'image') !== false) :
                        if(strpos($this->data['Cv']['picture']['type'], 'image') !== false) :
                            if ($this->Cv->save($this->data)) :
                                // Renomage de l'image et Upload de l'image
                                $exp = explode('.', $this->data['Cv']['picture']['name']);
                                $ext = $exp[count($exp)-1];
                                $this->data['Cv']['photo'] = $this->Cv->id.'.'.$ext;
                                if($this->Cv->save($this->data)) :
                                    $test['image'] = $this->data['Cv']['photo'];
                                    $this->FileUpload->renameFile($this->data['Cv']['picture']['name'], $this->data['Cv']['photo']);
                                endif;

                                if(!empty($this->data['Cv']['file']['type'])) :
                                    if((strpos($this->data['Cv']['file']['name'], '.pdf') !== false || strpos($this->data['Cv']['file']['name'], '.doc') !== false ) && $this->data['Cv']['file']['size'] <= 5000000) :
                                        // Renomage de l'image et Upload de l'image
                                        $exp = explode('.', $this->data['Cv']['file']['name']);
                                        $ext = $exp[count($exp)-1];
                                        $this->data['Cv']['import'] = $this->Cv->id.'.'.$ext;
                                        if($this->Cv->save($this->data)) :
                                            if ((isset($this->data['Cv']['import']['name']) && ($this->data['Cv']['file']['error'] == '0'))) {
                                                $chemin_destination = WWW_ROOT . '/files/';
                                                move_uploaded_file($this->data['Cv']['file']['tmp_name'], $chemin_destination.$this->data['Cv']['import']);
                                            }
                                        endif;
                                    endif;
                                endif;

                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord'])) :
                                        $id_profrecord1 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord'], $this->data['ProfessionalRecord']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['day'], $this->data['ProfessionalRecord']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['day'], $this->data['ProfessionalRecord']['company'], $this->data['ProfessionalRecord']['kind'])));
                                        $profrecord[] = $id_profrecord1;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord2'])) :
                                        $id_profrecord2 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord2'], $this->data['ProfessionalRecord']['started_at']['2']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['2']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['2']['day'], $this->data['ProfessionalRecord']['ended_at']['2']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['2']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['2']['day'], $this->data['ProfessionalRecord2']['company'], $this->data['ProfessionalRecord2']['kind'])));
                                        $profrecord[] = $id_profrecord2;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord3'])) :
                                        $id_profrecord3 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord3'], $this->data['ProfessionalRecord']['started_at']['3']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['3']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['3']['day'], $this->data['ProfessionalRecord']['ended_at']['3']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['3']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['3']['day'], $this->data['ProfessionalRecord3']['company'], $this->data['ProfessionalRecord3']['kind'])));
                                        $profrecord[] = $id_profrecord3;
                                endif;

                                unset ($this->data['ProfessionalRecord']);
                                if(isset($profrecord))
                                    $this->data['ProfessionalRecord'] = $profrecord;


                                if(!empty($this->data['Qualification']['Qualification'])) :
                                    $id_qualification1 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification'], $this->data['Qualification']['started_at']['year'].'-'.$this->data['Qualification']['started_at']['month'].'-'.$this->data['Qualification']['started_at']['day'], $this->data['Qualification']['ended_at']['year'].'-'.$this->data['Qualification']['ended_at']['month'].'-'.$this->data['Qualification']['ended_at']['day'])));
                                    $qualifi[] = $id_qualification1;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification2'])) :
                                    $id_qualification2 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification2'], $this->data['Qualification']['started_at']['2']['year'].'-'.$this->data['Qualification']['started_at']['2']['month'].'-'.$this->data['Qualification']['started_at']['2']['day'], $this->data['Qualification']['ended_at']['2']['year'].'-'.$this->data['Qualification']['ended_at']['2']['month'].'-'.$this->data['Qualification']['ended_at']['2']['day'])));
                                    $qualifi[] = $id_qualification2;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification3'])) :
                                    $id_qualification3 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification3'], $this->data['Qualification']['started_at']['3']['year'].'-'.$this->data['Qualification']['started_at']['3']['month'].'-'.$this->data['Qualification']['started_at']['3']['day'], $this->data['Qualification']['ended_at']['3']['year'].'-'.$this->data['Qualification']['ended_at']['3']['month'].'-'.$this->data['Qualification']['ended_at']['3']['day'])));
                                    $qualifi[] = $id_qualification3;
                                endif;
                                
                                unset ($this->data['Qualification']);
                                if(isset($qualifi)) :
                                    $this->data['Qualification'] = $qualifi;
                                endif;

                                $this->Cv->save($this->data);
                                $this->Session->setFlash(__('The cv has been saved', true));
                                $this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
                            else :
                                $this->Session->setFlash(__('The cv could not be saved. Please, try again.', true));
                            endif;
                        else :
                            $this->Session->setFlash(__('The cv could not be saved. Please, try again.', true));
                        endif;
                    else :
                        if ($this->Cv->save($this->data)) {
                                $this->Session->setFlash(__('The cv has been saved', true));

                                if(!empty($this->data['Cv']['file']['type'])) :
                                    if((strpos($this->data['Cv']['file']['name'], '.pdf') !== false || strpos($this->data['Cv']['file']['name'], '.doc') !== 0) && $this->data['Cv']['file']['size'] <= 5000000) :
                                        // Renomage de l'image et Upload de l'image
                                        $exp = explode('.', $this->data['Cv']['file']['name']);
                                        $ext = $exp[count($exp)-1];
                                        $this->data['Cv']['import'] = $this->Cv->id.'.'.$ext;
                                        if($this->Cv->save($this->data)) :
                                            if ((isset($this->data['Cv']['import']['name']) && ($this->data['Cv']['file']['error'] == '0'))) {
                                                $chemin_destination = WWW_ROOT . '/files/';
                                                move_uploaded_file($this->data['Cv']['file']['tmp_name'], $chemin_destination.$this->data['Cv']['import']);
                                            }
                                        endif;
                                    endif;
                                endif;
								
                                //Les expériences pro
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord'])) :
                                        $id_profrecord1 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord'], $this->data['ProfessionalRecord']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['day'], $this->data['ProfessionalRecord']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['day'], $this->data['ProfessionalRecord']['company'], $this->data['ProfessionalRecord']['kind'])));
                                        $profrecord[] = $id_profrecord1;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord2'])) :
                                        $id_profrecord2 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord2'], $this->data['ProfessionalRecord']['2']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['2']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['2']['started_at']['day'], $this->data['ProfessionalRecord']['2']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['2']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['2']['ended_at']['day'], $this->data['ProfessionalRecord2']['company'], $this->data['ProfessionalRecord2']['kind'])));
                                        $profrecord[] = $id_profrecord2;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord3'])) :
                                        $id_profrecord3 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord3'], $this->data['ProfessionalRecord']['3']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['3']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['3']['started_at']['day'], $this->data['ProfessionalRecord']['3']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['3']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['3']['ended_at']['day'], $this->data['ProfessionalRecord3']['company'], $this->data['ProfessionalRecord3']['kind'])));
                                        $profrecord[] = $id_profrecord3;
                                endif;

                                unset ($this->data['ProfessionalRecord']);
                                if(isset ($profrecord))
                                    $this->data['ProfessionalRecord'] = $profrecord;

                                // Les Diplomes
                                if(!empty($this->data['Qualification']['Qualification'])) :
                                    $id_qualification1 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification'], $this->data['Qualification']['started_at']['year'].'-'.$this->data['Qualification']['started_at']['month'].'-'.$this->data['Qualification']['started_at']['day'], $this->data['Qualification']['ended_at']['year'].'-'.$this->data['Qualification']['ended_at']['month'].'-'.$this->data['Qualification']['ended_at']['day'])));
                                    $qualifi[] = $id_qualification1;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification2'])) :
                                    $id_qualification2 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification2'], $this->data['Qualification']['started_at']['2']['year'].'-'.$this->data['Qualification']['started_at']['2']['month'].'-'.$this->data['Qualification']['started_at']['2']['day'], $this->data['Qualification']['ended_at']['2']['year'].'-'.$this->data['Qualification']['ended_at']['2']['month'].'-'.$this->data['Qualification']['ended_at']['2']['day'])));
                                    $qualifi[] = $id_qualification2;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification3'])) :
                                    $id_qualification3 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification3'], $this->data['Qualification']['started_at']['3']['year'].'-'.$this->data['Qualification']['started_at']['3']['month'].'-'.$this->data['Qualification']['started_at']['3']['day'], $this->data['Qualification']['ended_at']['3']['year'].'-'.$this->data['Qualification']['ended_at']['3']['month'].'-'.$this->data['Qualification']['ended_at']['3']['day'])));
                                    $qualifi[] = $id_qualification3;
                                endif;

                                unset ($this->data['Qualification']);
                                if(isset($qualifi)) :
                                    $this->data['Qualification'] = $qualifi;
                                endif;


                                // Sauvegarde des clés étrangères
                                $this->Cv->save($this->data);
                                $this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
                        } else {
                                $this->Session->setFlash(__('Le CV n\'a pas pu être enregistré', true));
                        }
                    endif;
		}

		$users = $this->Cv->User->find('list');
		$categories = $this->Cv->Category->find('list');
		$languages = $this->Cv->Language->find('list');
		$professionalRecords = $this->Cv->ProfessionalRecord->find('list');
		$qualifications = $this->Cv->Qualification->find('list');
		$skills = $this->Cv->Skill->find('list');
		$this->set(compact('users', 'categories', 'languages', 'professionalRecords', 'qualifications', 'skills'));
	}

	function edit($id = null) {
	
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cv', true));
			$this->redirect(array('action' => 'user'));
		}
		if (!empty($this->data)) {

                    $user = $this->Session->read('Auth.User');
                    $this->data['Cv']['title'] = $this->data['Cv']['title'];
                    $this->data['Cv']['interests'] = $this->data['Cv']['interests'];

                    //Les compétences

                    $this->requestAction(array(
                            'controller' => 'skills',
                            'action' => 'delete_by_cv'
                            ),
                            array(
                                'pass' => array(
                                                $this->Cv->id
                                            )));

                    if(!empty($this->data['Skill']['label'])):
                        $exp = explode(';',$this->data['Skill']['label']);
                        foreach ($exp as $skill):
                            if(!empty ($skill)) :
                                $idSkill = $this->requestAction(array(
                                                        'controller' => 'Skills',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $skill
                                                                    )));
                                $skills[] = $idSkill;
                            endif;
                        endforeach;
                        $this->data['Skill'] = $skills;
                    endif;

                    //Les exp pro

                    $this->requestAction(array(
                                    'controller' => 'ProfessionalRecords',
                                    'action' => 'delete_by_cv'
                                    ),
                                    array(
                                        'pass' => array(
                                                        $this->Cv->id
                                                    )));
                    //Les categories

                    $this->requestAction(array(
                                    'controller' => 'Categories',
                                    'action' => 'delete_by_cv'
                                    ),
                                    array(
                                        'pass' => array(
                                                        $this->Cv->id
                                                    )));
                    //Les diplomes

                    $this->requestAction(array(
                                    'controller' => 'Qualifications',
                                    'action' => 'delete_by_cv'
                                    ),
                                    array(
                                        'pass' => array(
                                                        $this->Cv->id
                                                    )));


                    //Les langues


                    $this->requestAction(array(
                                                'controller' => 'languages',
                                                'action' => 'delete_by_cv'
                                                ),
                                                array(
                                                    'pass' => array(
                                                                    $this->Cv->id
                                                                )));
                    

                    if(!empty ($this->data['Language']['label'])) :
                            $idLangue[] = $this->requestAction(array(
                                                        'controller' => 'languages',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $this->data['Language']['label'],
                                                                        $this->data['Language']['level']
                                                                    )));

                     endif;
                    if(!empty ($this->data['Language2']['label'])) :
                            $idLangue[] = $this->requestAction(array(
                                                        'controller' => 'languages',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $this->data['Language2']['label'],
                                                                        $this->data['Language2']['level']
                                                                    )));

                     endif;
                    if(!empty ($this->data['Language3']['label'])) :
                            $idLangue[] = $this->requestAction(array(
                                                        'controller' => 'languages',
                                                        'action' => 'add_by_label'
                                                        ),
                                                    array(
                                                        'pass' => array(
                                                                        $this->data['Language3']['label'],
                                                                        $this->data['Language3']['level']
                                                                    )));

                     endif;
                     unset($this->data['Language']);
                     unset($this->data['Language2']);
                     unset($this->data['Language3']);
                     if(isset ($idLangue))
                        $this->data['Language'] = $idLangue;




                    // Les catégories
                    if(isset($this->data['Category']['id']['0'])) :
                        $lesCategories = $this->data['Category']['id'];
                        unset ($this->data['Category']);
                        $this->data['Category'] = $lesCategories;
                    endif;


                    $this->data['Cv']['user_id'] = $user['id'];
                    if(!empty($this->data['Cv']['picture']['type']) && strpos($this->data['Cv']['picture']['type'], 'image') !== false) :
                        if(strpos($this->data['Cv']['picture']['type'], 'image') !== false) :
                            if ($this->Cv->save($this->data)) :
                                // Renomage de l'image et Upload de l'image
                                $exp = explode('.', $this->data['Cv']['picture']['name']);
                                $ext = $exp[count($exp)-1];
                                $this->data['Cv']['photo'] = $this->Cv->id.'.'.$ext;
                                if($this->Cv->save($this->data)) :
                                    $test['image'] = $this->data['Cv']['photo'];
                                    $this->FileUpload->renameFile($this->data['Cv']['picture']['name'], $this->data['Cv']['photo']);
                                endif;

                                if(!empty($this->data['Cv']['file']['type'])) :
                                    if((strpos($this->data['Cv']['file']['name'], '.pdf') !== false || strpos($this->data['Cv']['file']['name'], '.doc') !== false) && $this->data['Cv']['file']['size'] <= 5000000) :
                                        // Renomage de l'image et Upload de l'image
                                        $exp = explode('.', $this->data['Cv']['file']['name']);
                                        $ext = $exp[count($exp)-1];
                                        $this->data['Cv']['import'] = $this->Cv->id.'.'.$ext;
                                        if($this->Cv->save($this->data)) :
                                            if ((isset($this->data['Cv']['import']['name']) && ($this->data['Cv']['file']['error'] == '0'))) {
                                                $chemin_destination = WWW_ROOT . '/files/';
                                                move_uploaded_file($this->data['Cv']['file']['tmp_name'], $chemin_destination.$this->data['Cv']['import']);
                                            }
                                        endif;
                                    endif;
                                endif;


                                //Les exp pro

                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord'])) :
                                        $id_profrecord1 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord'], $this->data['ProfessionalRecord']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['day'], $this->data['ProfessionalRecord']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['day'], $this->data['ProfessionalRecord']['company'], $this->data['ProfessionalRecord']['kind'])));
                                        $profrecord[] = $id_profrecord1;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord2'])) :
                                        $id_profrecord2 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord2'], $this->data['ProfessionalRecord']['started_at']['2']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['2']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['2']['day'], $this->data['ProfessionalRecord']['ended_at']['2']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['2']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['2']['day'], $this->data['ProfessionalRecord2']['company'], $this->data['ProfessionalRecord2']['kind'])));
                                        $profrecord[] = $id_profrecord2;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord3'])) :
                                        $id_profrecord3 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord3'], $this->data['ProfessionalRecord']['started_at']['3']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['3']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['3']['day'], $this->data['ProfessionalRecord']['ended_at']['3']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['3']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['3']['day'], $this->data['ProfessionalRecord3']['company'], $this->data['ProfessionalRecord3']['kind'])));
                                        $profrecord[] = $id_profrecord3;
                                endif;

                                unset ($this->data['ProfessionalRecord']);
                                if(isset($profrecord))
                                    $this->data['ProfessionalRecord'] = $profrecord;


                                if(!empty($this->data['Qualification']['Qualification'])) :
                                    $id_qualification1 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification'], $this->data['Qualification']['started_at']['year'].'-'.$this->data['Qualification']['started_at']['month'].'-'.$this->data['Qualification']['started_at']['day'], $this->data['Qualification']['ended_at']['year'].'-'.$this->data['Qualification']['ended_at']['month'].'-'.$this->data['Qualification']['ended_at']['day'])));
                                    $qualifi[] = $id_qualification1;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification2'])) :
                                    $id_qualification2 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification2'], $this->data['Qualification']['started_at']['2']['year'].'-'.$this->data['Qualification']['started_at']['2']['month'].'-'.$this->data['Qualification']['started_at']['2']['day'], $this->data['Qualification']['ended_at']['2']['year'].'-'.$this->data['Qualification']['ended_at']['2']['month'].'-'.$this->data['Qualification']['ended_at']['2']['day'])));
                                    $qualifi[] = $id_qualification2;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification3'])) :
                                    $id_qualification3 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification3'], $this->data['Qualification']['started_at']['3']['year'].'-'.$this->data['Qualification']['started_at']['3']['month'].'-'.$this->data['Qualification']['started_at']['3']['day'], $this->data['Qualification']['ended_at']['3']['year'].'-'.$this->data['Qualification']['ended_at']['3']['month'].'-'.$this->data['Qualification']['ended_at']['3']['day'])));
                                    $qualifi[] = $id_qualification3;
                                endif;

                                unset ($this->data['Qualification']);
                                if(isset($qualifi)) :
                                    $this->data['Qualification'] = $qualifi;
                                endif;

                                $this->Cv->save($this->data);
                                $this->Session->setFlash(__('The cv has been saved', true));
                                $this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
                            else :
                                $this->Session->setFlash(__('The cv could not be saved. Please, try again.', true));
                            endif;
                        else :
                            $this->Session->setFlash(__('The cv could not be saved. Please, try again.', true));
                        endif;
                    else :
                        if ($this->Cv->save($this->data)) {
                                $this->Session->setFlash(__('The cv has been saved', true));

                                if(!empty($this->data['Cv']['file']['type'])) :
                                    if((strpos($this->data['Cv']['file']['name'], '.pdf') !== false || strpos($this->data['Cv']['file']['name'], '.doc') !== false) && $this->data['Cv']['file']['size'] <= 5000000) :
                                        // Renomage de l'image et Upload de l'image
                                        $exp = explode('.', $this->data['Cv']['file']['name']);
                                        $ext = $exp[count($exp)-1];
                                        $this->data['Cv']['import'] = $this->Cv->id.'.'.$ext;
                                        if($this->Cv->save($this->data)) :
                                            if ((isset($this->data['Cv']['import']['name']) && ($this->data['Cv']['file']['error'] == '0'))) {
                                                $chemin_destination = WWW_ROOT . '/files/';
                                                move_uploaded_file($this->data['Cv']['file']['tmp_name'], $chemin_destination.$this->data['Cv']['import']);
                                            }
                                        endif;
                                    endif;
                                endif;
                                print_r($this->data['Cv']['file']);
								
                                //Les expériences pro
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord'])) :
                                        $id_profrecord1 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord'], $this->data['ProfessionalRecord']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['started_at']['day'], $this->data['ProfessionalRecord']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['ended_at']['day'], $this->data['ProfessionalRecord']['company'], $this->data['ProfessionalRecord']['kind'])));
                                        $profrecord[] = $id_profrecord1;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord2'])) :
                                        $id_profrecord2 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord2'], $this->data['ProfessionalRecord']['2']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['2']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['2']['started_at']['day'], $this->data['ProfessionalRecord']['2']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['2']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['2']['ended_at']['day'], $this->data['ProfessionalRecord2']['company'], $this->data['ProfessionalRecord2']['kind'])));
                                        $profrecord[] = $id_profrecord2;
                                endif;
                                if(!empty($this->data['ProfessionalRecord']['ProfessionalRecord3'])) :
                                        $id_profrecord3 = $this->requestAction(array('controller' => 'ProfessionalRecords', 'action' => 'add_by_label'),  array('pass' => array($this->data['ProfessionalRecord']['ProfessionalRecord3'], $this->data['ProfessionalRecord']['3']['started_at']['year'].'-'.$this->data['ProfessionalRecord']['3']['started_at']['month'].'-'.$this->data['ProfessionalRecord']['3']['started_at']['day'], $this->data['ProfessionalRecord']['3']['ended_at']['year'].'-'.$this->data['ProfessionalRecord']['3']['ended_at']['month'].'-'.$this->data['ProfessionalRecord']['3']['ended_at']['day'], $this->data['ProfessionalRecord3']['company'], $this->data['ProfessionalRecord3']['kind'])));
                                        $profrecord[] = $id_profrecord3;
                                endif;

                                unset ($this->data['ProfessionalRecord']);
                                if(isset ($profrecord))
                                    $this->data['ProfessionalRecord'] = $profrecord;

                                // Les Diplomes
                                if(!empty($this->data['Qualification']['Qualification'])) :
                                    $id_qualification1 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification'], $this->data['Qualification']['started_at']['year'].'-'.$this->data['Qualification']['started_at']['month'].'-'.$this->data['Qualification']['started_at']['day'], $this->data['Qualification']['ended_at']['year'].'-'.$this->data['Qualification']['ended_at']['month'].'-'.$this->data['Qualification']['ended_at']['day'])));
                                    $qualifi[] = $id_qualification1;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification2'])) :
                                    $id_qualification2 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification2'], $this->data['Qualification']['started_at']['2']['year'].'-'.$this->data['Qualification']['started_at']['2']['month'].'-'.$this->data['Qualification']['started_at']['2']['day'], $this->data['Qualification']['ended_at']['2']['year'].'-'.$this->data['Qualification']['ended_at']['2']['month'].'-'.$this->data['Qualification']['ended_at']['2']['day'])));
                                    $qualifi[] = $id_qualification2;
                                endif;
                                if(!empty($this->data['Qualification']['Qualification3'])) :
                                    $id_qualification3 = $this->requestAction(array('controller' => 'qualifications', 'action' => 'add_by_label'),  array('pass' => array($this->data['Qualification']['Qualification3'], $this->data['Qualification']['started_at']['3']['year'].'-'.$this->data['Qualification']['started_at']['3']['month'].'-'.$this->data['Qualification']['started_at']['3']['day'], $this->data['Qualification']['ended_at']['3']['year'].'-'.$this->data['Qualification']['ended_at']['3']['month'].'-'.$this->data['Qualification']['ended_at']['3']['day'])));
                                    $qualifi[] = $id_qualification3;
                                endif;

                                unset ($this->data['Qualification']);
                                if(isset($qualifi)) :
                                    $this->data['Qualification'] = $qualifi;
                                endif;


                                // Sauvegarde des clés étrangères
                                $this->Cv->save($this->data);
                                $this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
                        } else {
                                $this->Session->setFlash(__('Le CV n\'a pas pu être enregistré', true));
                        }
                    endif;
		}
		if (empty($this->data)) {
			$this->data = $this->Cv->read(null, $id);
		}
		$categories = $this->Cv->Category->find('list');
                $languages = $this->Cv->query('SELECT l.*
                                              FROM cvs c, languages_cvs lc, languages l
                                              WHERE c.id = lc.cv_id
                                              AND lc.language_id = l.id
                                              AND c.id='.$this->Cv->id);
                $ifLanguage = count($languages);
                if($ifLanguage > 0):
                    $i = 0;
                    foreach($languages as $lang) :
                        $maLang[$i]['id']= $lang['l']['id'];
                        $maLang[$i]['label']= $lang['l']['label'];
                        $maLang[$i]['level']= $lang['l']['level'];
                        $i++;
                    endforeach;
                endif;
                $listProfessionalRecords = $this->Cv->query('SELECT p.*
                                              FROM cvs c, professional_records_cvs pc, professional_records p
                                              WHERE c.id = pc.cv_id
                                              AND pc.professional_record_id = p.id
                                              AND c.id='.$this->Cv->id);
                $ifPc = count($listProfessionalRecords);
                if($ifPc > 0):
                    $i = 0;
                    foreach($listProfessionalRecords as $pc) :
                        $professionalRecords[$i]['id']= $pc['p']['id'];
                        $professionalRecords[$i]['job']= $pc['p']['job'];
                        $professionalRecords[$i]['company']= $pc['p']['company'];
                        $professionalRecords[$i]['kind']= $pc['p']['kind'];
                        $professionalRecords[$i]['started_at']= $pc['p']['started_at'];
                        $professionalRecords[$i]['ended_at']= $pc['p']['ended_at'];
                        $i++;
                    endforeach;
                endif;
                $listQualifications = $this->Cv->query('SELECT q.*
                                              FROM cvs c, qualifications_cvs qc, qualifications q
                                              WHERE c.id = qc.cv_id
                                              AND qc.qualification_id = q.id
                                              AND c.id='.$this->Cv->id);
                $ifQc = count($listQualifications);
                if($ifQc > 0):
                    $i = 0;
                    foreach($listQualifications as $qc) :
                        $qualifications[$i]['id']= $qc['q']['id'];
                        $qualifications[$i]['label']= $qc['q']['label'];
                        $qualifications[$i]['started_at']= $qc['q']['started_at'];
                        $qualifications[$i]['ended_at']= $qc['q']['ended_at'];
                        $i++;
                    endforeach;
                endif;
                $listSkills = $this->Cv->query('SELECT s.*
                                              FROM cvs c, skills_cvs sc, skills s
                                              WHERE c.id = sc.cv_id
                                              AND sc.skill_id = s.id
                                              AND c.id='.$this->Cv->id);
                $ifSc = count($listSkills);
                if($ifSc > 0):
                    $i = 0;
                    foreach($listSkills as $sc) :
                        if($i == 0) :
                            $skills = $sc['s']['label'];
                        else :
                            $skills .= ';'.$sc['s']['label'];
                        endif;
                        $i++;
                    endforeach;
                endif;
                $listCat = $this->Cv->query('SELECT ct.*
                                              FROM cvs c, categories_cvs cc, categories ct
                                              WHERE c.id = cc.cv_id
                                              AND cc.category_id = ct.id
                                              AND c.id='.$this->Cv->id);
                $ifCc = count($listCat);
                if($ifCc > 0):
                    $i = 0;
                    foreach($listCat as $cc) :
                            $selectedCat[] = $cc['ct']['id'];
                        $i++;
                    endforeach;
                endif;

                $leCV = $this->Cv->findById($id);
                $photo    = $leCV['Cv']['photo'];
                $cv    = $leCV['Cv']['import'];
		$this->set(compact('id','users', 'photo', 'cv', 'categories', 'languages', 'professionalRecords', 'qualifications', 'skills', 'selectedCat', 'maLang'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cv', true));
			$this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
		}
		if ($this->Cv->delete($id)) {
			$this->Session->setFlash(__('Cv deleted', true));
			$this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
		}
		$this->Session->setFlash(__('Cv was not deleted', true));
		$this->redirect(array('action' => 'user', $this->Session->read('Auth.User.id')));
	}

        function delete_image($id = null){
            $this->autoRender = false;
            if(!empty ($id)) :
                $ifCV = $this->Cv->find('all', array('conditions' => array('Cv.id' => $id, 'user_id' => $this->Session->read('Auth.User.id'))));
                if(count($ifCV) > 0) :
                    $this->Cv->query('UPDATE cvs SET photo ="" WHERE id ='.$id);
                    $this->FileUpload->removeFile($ifCV[0]['Cv']['photo']);
                endif;
            endif;
        }

        function delete_cv($id = null){
            $this->autoRender = false;
            if(!empty ($id)) :
                $ifCV = $this->Cv->find('all', array('conditions' => array('Cv.id' => $id, 'user_id' => $this->Session->read('Auth.User.id'))));
                if(count($ifCV) > 0) :
                    $this->Cv->query('UPDATE cvs SET import = "" WHERE id = '.$id);
                    $this->FileUpload->removeFile($ifCV[0]['Cv']['import']);
                endif;
            endif;
        }
        
	function admin_index() {
		$this->Cv->recursive = 0;
		$this->set('cvs', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cv', $this->Cv->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Cv->create();
			if ($this->Cv->save($this->data)) {
				$this->Session->setFlash(__('The cv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cv could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Cv->User->find('list');
		$categories = $this->Cv->Category->find('list');
		$languages = $this->Cv->Language->find('list');
		$professionalRecords = $this->Cv->ProfessionalRecord->find('list');
		$qualifications = $this->Cv->Qualification->find('list');
		$skills = $this->Cv->Skill->find('list');
		$this->set(compact('users', 'categories', 'languages', 'professionalRecords', 'qualifications', 'skills'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Cv->save($this->data)) {
				$this->Session->setFlash(__('The cv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cv->read(null, $id);
		}
		$users = $this->Cv->User->find('list');
		$categories = $this->Cv->Category->find('list');
		$languages = $this->Cv->Language->find('list');
		$professionalRecords = $this->Cv->ProfessionalRecord->find('list');
		$qualifications = $this->Cv->Qualification->find('list');
		$skills = $this->Cv->Skill->find('list');
		$this->set(compact('users', 'categories', 'languages', 'professionalRecords', 'qualifications', 'skills'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Cv->delete($id)) {
			$this->Session->setFlash(__('Cv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Cv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	




}
?>